<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/component/jsoneditor/css/jsoneditor.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
        <style>
            .layui-form-label{
                width: 100px;
            }
            .layui-input-block{
                margin-left:130px;
            }
            table thead tr{
                background-color: #f2f2f2;
            }
            table tbody .layui-table-cell{
                height: 50px;
                position: relative;
                box-sizing: border-box;
            }
        </style>
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">用户级别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="agent_level" value="1" title="V1" lay-filter="agent_level" checked>
                            <input type="radio" name="agent_level" value="2" title="V2" lay-filter="agent_level" >
                            <input type="radio" name="agent_level" value="3" title="V3" lay-filter="agent_level" >
                        </div>
                    </div>
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label required">星图达人佣金</label>-->
                        <!--<div class="layui-input-block">-->
                            <!--<input type="number" name="talent_commission_rate" value="0" lay-verify="required" class="layui-input">-->
                            <!--<tip style="color: red;font-size: 12px;">整数 70 表示70% 佣金70-79范围 为0走默认比例70%</tip>-->
                        <!--</div>-->
                    <!--</div>-->
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label required">星图直推佣金</label>-->
                        <!--<div class="layui-input-block">-->
                            <!--<input type="number" name="direct_commission_rate" value="0" lay-verify="required" class="layui-input">-->
                            <!--<tip style="color: red;font-size: 12px;">整数 70 表示70% 为0走默认比例2%</tip>-->
                        <!--</div>-->
                    <!--</div>-->
                    <div class="layui-form-item">
                        <label class="layui-form-label required">佣金比例</label>
                        <div class="layui-input-block">
                            <table class="layui-hide" id="currentTableIdAttr" lay-filter="currentTableIdAttr"></table>
                        </div>
                    </div>
                    <!--<div class="layui-form-item">-->
                        <!--<label class="layui-form-label required">奖励策略</label>-->
                        <!--<div class="layui-input-block">-->
                            <!--<div name="reward_plan_id" id="reward_plan_id" value=""></div>-->
                        <!--</div>-->
                    <!--</div>-->
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <!--<button type="reset" class="pear-btn pear-btn-md">-->
                        <!--重置-->
                    <!--</button>-->
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/component/jsoneditor/jsoneditor.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/functions.js"></script>
        <script src="/app/admin/admin/js/md5.js"></script>
        
        <script>
            let table;
            let commission_plan = [];

            // 相关接口
            const PRIMARY_KEY = "userid";
            const PRIMARY_KEY_VALUE = layui.url().search[PRIMARY_KEY];
            const SELECT_API = "/app/admin/user/index/select" + location.search;
            const UPDATE_API = "/app/admin/user/index/update";


            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    async:true,
                    headers: get_header({"userid":PRIMARY_KEY_VALUE}),
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(value);
                            }else if (typeof $(obj[0]).attr('type')!=="undefined" && $(obj[0]).attr('type').toLowerCase() === "radio") {
                                $('input[name="'+key+'"]').each(function (i,v) {
                                    if(value==$(this).val()){
                                        $(this).attr("checked","checked");
                                    }else{
                                        $(this).removeAttr("checked");
                                    }
                                })
                            } else {
                                obj.attr("value", value);
                                obj[0].value = value;
                            }
                        });

                        //getRewardList(res.data[0].agent_level)

                        commission_plan = res.data[0].commission_plan;
                        layui.use(["table", "form", "common", "popup", "util"], function() {
                            table = layui.table;
                            table_render();

                        });

                        // ajax返回失败
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                        layui.form.render();
                    }
                });

                layui.form.on('radio(agent_level)', function(data){
                    getRewardList(data.value);
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    data.field['commission_plan'] = commission_plan;
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        async:true,
                        headers: get_header(data.field),
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });

                window.table_render = function() {
                    //表格渲染
                    table.render({
                        elem: '#currentTableIdAttr'
                        //, toolbar: '#toolbarAttr'
                        , title: '列表'
                        , text: {
                            none: '暂无相关数据'
                        }
                        //, width:950
                        , defaultToolbar: []
                        , cols: [[ //表头
                            {field: 'promotion_project', title: '推广项目', width: 150}
                            , {field: 'talent_commission_rate', title: '达人比例',templet: function (d) {
                                    return ' <input type="number" name="talent_commission_rate" value="' + d.talent_commission_rate + '"  data-promotion_project="'+d.promotion_project+'" data-name="talent_commission_rate" onchange="change_value(this)" class="layui-input">';
                                }}

                            , {field: 'direct_commission_rate', title: '直推比例', width: 150,templet: function (d) {
                                    return ' <input type="number" name="direct_commission_rate" value="' + d.direct_commission_rate + '"  data-promotion_project="'+d.promotion_project+'" data-name="direct_commission_rate" onchange="change_value(this)" class="layui-input">';
                                }}
                        ]]
                        , data: commission_plan
                        , limit: 10
                    });
                }

                window.change_value = function($this) {
                    var promotion_project = $($this).data('promotion_project');
                    var name = $($this).data('name');
                    var value = $($this).val();

                    $.each(commission_plan,function (i,v) {
                        if(v!=undefined && promotion_project==v.promotion_project){
                            if(name=='talent_commission_rate'){
                                commission_plan[i].talent_commission_rate = value;
                            }else if(name=='direct_commission_rate'){
                                commission_plan[i].direct_commission_rate = value;
                            }
                        }
                    })
                    table_render();
                }
            });

            function getRewardList(agent_level) {
                // 字段 商家 business_id
                layui.use(["jquery", "xmSelect", "popup"], function() {
                    layui.$.ajax({
                        url: "/app/admin/reward/index/select?format=select&limit=1000&agent_level="+agent_level,
                        dataType: "json",
                        async:true,
                        headers: get_header({"format":"select","agent_level":agent_level}),
                        success: function (res) {
                            let value = layui.$("#reward_plan_id").attr("value");
                            let initValue = value ? value.split(",") : [];
                            layui.xmSelect.render({
                                el: "#reward_plan_id",
                                name: "reward_plan_id",
                                filterable: true,
                                initValue: initValue,
                                data: res.data,
                                model: {"icon":"hidden","label":{"type":"text"}},
                                clickClose: true,
                                radio: true,
                            })
                            if (res.code) {
                                layui.popup.failure(res.msg);
                            }
                        }
                    });
                });
            }
        </script>

    </body>

</html>
